/*
 *    This file is part of SSHTunneler.
 *
 *    Copyright 2009 Jean-Sebastien Gelinas <calestar@gmail.com>
 *
 *    SSHTunneler is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    SSHTunneler is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with SSHTunneler.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
package sshtunneler.tools;

import java.awt.MenuItem;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;

import javax.swing.JMenuItem;
import javax.swing.JOptionPane;

public class HTTPRedirectWriterTool extends Tool {

	JMenuItem mnu_tunnels = new JMenuItem("Write HTTP redirect file");
	MenuItem mnu_tray = new MenuItem("Write HTTP redirect file");

	public HTTPRedirectWriterTool() {
		super("HTTPRedirectWriterTool", "HTTPRedirectWriterTool");
	}

	public void write_to(File file, String page) throws IOException {
		FileOutputStream stream = new FileOutputStream(file);
		PrintStream out = new PrintStream(stream);

		this.write_to(out, page);

		out.close();
		stream.close();
	}

	private void write_to(PrintStream out, String page) throws IOException {
		out.println("HTML>");
		out.println("<HEAD>");
		out.println("<script>");
		out.println("<!--");
		out.println("function redirect(path)");
		out.println("{");
		out
				.println("	var red = setTimeout(\"location.href='\"+path+\"'\", 1000);");
		out.println("}");
		out.println("//-->");
		out.println("</script>");
		out.println("</HEAD>");
		out.println("<BODY onLoad=\"redirect('" + page + "')\">");
		out
				.println("<br><br><center>Redirecting to page: <a href=\"'+path+'\">' + path + '</a></center>");
		out.println("</BODY>");
		out.println("</HTML>");
	}

	@Override
	public JMenuItem get_tunnel_tools_menu(boolean server_connected,
			boolean tunnel_connected) {
		return this.mnu_tunnels;
	}

	@Override
	public JMenuItem get_server_tools_menu(boolean server_connected) {
		return null;
	}

	@Override
	public MenuItem get_tray_tools_menu() {
		return this.mnu_tray;
	}

	@Override
	public JMenuItem get_main_tools_menu() {
		return null;
	}

	@Override
	public void run() {
		File selection = this.ask_file(".html", "HTML Files");
		String target = "localhost:" + this.currentTunnel.getLocalPort() + "/"
				+ this.getProperty("target_page");
		try {
			this.write_to(selection, target);
			JOptionPane.showMessageDialog(null, "File written.");
		} catch (IOException ex) {
			JOptionPane.showMessageDialog(null, ex.getMessage(),
					"Writing aborted ...", JOptionPane.ERROR_MESSAGE);
		}
	}

	@Override
	public void init_properties() {
		this.properties.put("target_page", "index.html");
	}
}
